<?php
class PayAction extends BaseAction{
	
	  //在类初始化方法中，引入相关类库    
       public function _initialize() {
        vendor('Alipay.Corefunction');
        vendor('Alipay.Md5function');
        vendor('Alipay.Notify');
        vendor('Alipay.Submit');    
    }
	
	function dopay(){
        $alipay_config=C('alipay_config');
        $alipay =C('alipay');
        $payment_type = "1";
        $notify_url = $alipay['notify_url'];
        $return_url = $alipay['return_url'];
        $seller_email = $alipay['seller_email'];
        $out_trade_no = $_GET['WIDout_trade_no'];//订单号
        $subject = $_GET['WIDsubject'];//订单名称
        $total_fee = $_GET['WIDtotal_fee']; //付款金额
        $body = $_GET['WIDbody']; //订单描述
        $show_url = 'http://www.yrs.cc';//商品地址
        $anti_phishing_key = "";
        $exter_invoke_ip = get_client_ip();
        
		$parameter = array(
				"service" => "create_direct_pay_by_user",
				"partner" => trim($alipay_config['partner']),
				"payment_type"	=> $payment_type,
				"notify_url"	=> $notify_url,
				"return_url"	=> $return_url,
				"seller_email"	=> $seller_email,
				"out_trade_no"	=> $out_trade_no,
				"subject"	=> $subject,
				"total_fee"	=> $total_fee,
				"body"	=> $body,
				"show_url"	=> $show_url,
				"anti_phishing_key"	=> $anti_phishing_key,
				"exter_invoke_ip"	=> $exter_invoke_ip,
				"_input_charset"	=> trim(strtolower($alipay_config['input_charset']))
		);
		$alipaySubmit = new AlipaySubmit($alipay_config);
		$html_text = $alipaySubmit->buildRequestForm($parameter,"get", "确认");
		header("Content-type: text/html; charset=utf-8");
		echo $html_text;
	}
	
	function notifyurl(){
		$alipay_config=C('alipay_config');
		//计算得出通知验证结果
      $alipayNotify = new AlipayNotify($alipay_config);
      $verify_result = $alipayNotify->verifyNotify();
	
       if($verify_result) {
       	   $out_trade_no = $_POST['out_trade_no'];//商户订单
       	   $trade_no = $_POST['trade_no']; //支付宝交易号
       	   $trade_status = $_POST['trade_status'];//交易状态
       if($_POST['trade_status'] == 'TRADE_FINISHED') {

          }
           else if ($_POST['trade_status'] == 'TRADE_SUCCESS') {
                 //不做处理
                  
             } 
         	echo "success";		//请不要修改或删除
       }else{
       	 echo "fail";
       }
 
		
	}
	
	function returnurl(){
		header("Content-type: text/html; charset=utf-8");
				$alipay_config=C('alipay_config');
				   
				$alipayNotify = new AlipayNotify($alipay_config);
                 $verify_result = $alipayNotify->verifyReturn();
           
                 if($verify_result){
                 	$out_trade_no = $_GET['out_trade_no']; //商户订单号

				
					//交易状态
					$trade_status = $_GET['trade_status'];
                  
				    if($_GET['trade_status'] == 'TRADE_FINISHED' || $_GET['trade_status'] == 'TRADE_SUCCESS') {
				    	//echo("订单号：".$out_trade_no."支付宝交易号：".$trade_no);

						  	M('Order')->where("orderID='".$out_trade_no."'")->setField('status', 1);
						  	$orderinfo =M('Order')->where("orderID='".$out_trade_no."'")->find();
						  	//更新用户金币
						  	$price = $orderinfo['price'] * 10;
						  	 M('User')->where("uid=".$orderinfo['uid'])->setInc("price",$price);
                        $url = U('User/index');
                        header('Location:'.$url);
						 // $this->success("支付成功3秒后跳转到用户中心",U('User/index'));
				    }
				    else {
				      echo "trade_status=".$_GET['trade_status'];
				       exit("支付失败  <a href=".U('uc/orders').">点击返回用户中心</a>");
				    }
						
					
                 }else{
                 	
                 	 echo "验证失败";
                 }
		
	}
	
	
}
?>